filepath = "/home/CAMPUS/bawa2018/Climate_Change_Narratives/Data/FA20/Williams_SaltLakeCityUT_data.csv"
climate_data = read.csv(filepath)
strDates <- as.character(climate_data$DATE)
climate_data$NewDate <- as.Date(strDates, "%Y-%m-%d")
plot(TMAX~NewDate, climate_data, pch = 16, cex=.2, col = "blue")
TMAX.lm = lm(TMAX ~ NewDate, data = climate_data) 
coef(TMAX.lm)
##  (Intercept)      NewDate 
## 1.768352e+01 4.390910e-05
abline(coef(TMAX.lm),col ="orange", lwd = 3)

climate_data$Month = format(as.Date(climate_data$NewDate), format = "%m")
climate_data$Year = format(climate_data$NewDate, format="%Y")
MonthlyTMAXMean = aggregate(TMAX ~ Month + Year, climate_data, mean)
MonthlyTMAXMean$YEAR = as.numeric(MonthlyTMAXMean$Year)
MonthlyTMAXMean$MONTH = as.numeric(MonthlyTMAXMean$Month)
plot(MonthlyTMAXMean$TMAX, ty='l')

#plot(MonthlyTMAXMean£TMAX[MonthlyTMAXMean£Month=="05"], ty='l')
plot(TMAX~YEAR, data=MonthlyTMAXMean[MonthlyTMAXMean$Month=="05",],ty='l', xlim=c(1950, 2020))
May.lm <- lm(TMAX~YEAR, data=MonthlyTMAXMean[MonthlyTMAXMean$Month=="05",])
summary(May.lm)
## 
## Call:
## lm(formula = TMAX ~ YEAR, data = MonthlyTMAXMean[MonthlyTMAXMean$Month == 
##     "05", ])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.8792 -1.5588  0.3994  1.6796  4.7565 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)
## (Intercept) 18.275162  23.794808   0.768    0.445
## YEAR         0.002012   0.011993   0.168    0.867
## 
## Residual standard error: 2.159 on 71 degrees of freedom
## Multiple R-squared:  0.0003962,  Adjusted R-squared:  -0.01368 
## F-statistic: 0.02814 on 1 and 71 DF,  p-value: 0.8672
abline(coef(May.lm), col="red")

MonthlyTMINMean = aggregate(TMIN ~ Month + Year, climate_data, mean)
MonthlyTMINMean$YEAR = as.numeric(MonthlyTMINMean$Year)
# Fixing the Format of Month and Year as numeric
MonthlyTMINMean$YEAR = as.numeric(MonthlyTMINMean$Year)
MonthlyTMINMean$MONTH = as.numeric(MonthlyTMINMean$Month)
head(MonthlyTMINMean)
##   Month Year      TMIN YEAR MONTH
## 1    01 1948 -6.183871 1948     1
## 2    02 1948 -4.727586 1948     2
## 3    03 1948 -3.093548 1948     3
## 4    04 1948  3.580000 1948     4
## 5    05 1948  6.990323 1948     5
## 6    06 1948 12.203333 1948     6
# First I create a vector of months
Months = c("January", "February", "March", "April",
"May", "June", "July", "August", "September", "October",
"November", "December")
# Create a panel so I can see all the figures at
# once.
par(mfrow = c(4, 3), mar = c(5, 4, 3, 2) + 0.1)
TMAXresult <-NA; TMINresult <- NA
for (i in 1:12) {
    # plot(MonthlyTMAXMean£TMAX[MonthlyTMAXMean£Month==i],
    # ty='l')
    plot(TMAX ~ YEAR, data = MonthlyTMAXMean[MonthlyTMAXMean$MONTH == i, ], ty = "l", las = 1, xlim = c(1940, 2020), main = Months[i])
  Month.lm <- lm(TMAX ~ YEAR, data = MonthlyTMAXMean[MonthlyTMAXMean$MONTH == i, ])
  summary(Month.lm)
  abline(coef(Month.lm), col = "red")
  TMAXresult <- rbind(TMAXresult, cbind(Months[i],
  round(coef(Month.lm)[2], 4), round(summary(Month.lm)$coefficients[2, 4], 4), round(summary(Month.lm)$r.squared,3)))
  }

#par(mfrow=c(4,3),mar=c(5,4,1,1))
for (i in 1:12) {
MonthMin_lm <- lm(TMIN ~ YEAR, data=MonthlyTMINMean[MonthlyTMINMean$MONTH == i, ])
TMINresult <- rbind(TMINresult, cbind(Months[i],round(coef(MonthMin_lm)[2], 4), round(summary(MonthMin_lm)$coefficients[2,4], 4), round(summary(MonthMin_lm)$r.squared, 3)))
summary(MonthMin_lm)
plot(MonthlyTMINMean$TMIN, ty='l', ylab='Monthly avg min temp', xlab='Years',main=Months[i]
)
abline(coef(MonthMin_lm),col='blue')
}

library(htmlTable)
Results <- data.frame(Month = TMINresult[c(2:13),1],
TMINSlope = TMINresult[c(2:13),2],
TMIN_P = as.numeric(TMINresult[c(2:13),3]),
TMINRsq = TMINresult[c(2:13),4],
TMAXSlope = TMAXresult[c(2:13),2],
TMAX_P = as.numeric(TMAXresult[c(2:13),3]),
TMAXRsq = TMAXresult[c(2:13),4])
Results$starTMIN = "NS"
Results$starTMIN[Results$TMIN_P <= .05] = "*"
Results$starTMIN[Results$TMIN_P < 0.01] = "**"
Results$starTMIN[Results$TMIN_P < 0.001] = "***"
Results$starTMAX = "NS"
Results$starTMAX[Results$TMAX_P < 0.05] = "*"
Results$starTMAX[Results$TMAX_P < 0.01] = "**"
Results$starTMAX[Results$TMAX_P < 0.001] = "***"
Results$TMINslope=paste(Results$TMINSlope, Results$starTMIN)
Results$TMAXslope=paste(Results$TMAXSlope, Results$starTMAX)
colnames(Results) <- c("Month", "2", "3", "R^2", "5", "6",
"R^2", "8", "9", "Slope TMIN", "Slope TMAX")
htmlTable(Results[,c(1, 10, 4, 11, 7)])
Month Slope TMIN R^2 Slope TMAX R^2.1
1 January 0.0454 * 0.081 0.0148 NS 0.011
2 February 0.039 ** 0.097 0.0168 NS 0.016
3 March 0.0661 *** 0.434 0.0535 *** 0.236
4 April 0.0381 *** 0.245 0.0069 NS 0.005
5 May 0.0435 *** 0.29 0.002 NS 0
6 June 0.0604 *** 0.434 0.0247 * 0.054
7 July 0.0688 *** 0.588 0.019 * 0.07
8 August 0.0624 *** 0.454 0.0176 * 0.076
9 September 0.0605 *** 0.429 0.0118 NS 0.015
10 October 0.0352 *** 0.198 -0.0132 NS 0.017
11 November 0.0343 ** 0.141 0.0098 NS 0.008
12 December 0.0217 NS 0.039 -9e-04 NS 0